/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author dtuanh
 */
// Server.java
// Cours CORBA
// Cours 1, Exemple 1
// avec naming service
// On startup, the client lookup the object reference from the
// COS naming service.
//
// The name which the object is bound to is as follows:
//       root [context]
//        |
//       Hello [context] kind [hello_context]
//        |
//       HelloWorld [object] kind [Object]
//

import Dict.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import org.omg.CORBA.ORBPackage.*;
import java.io.*;
// le serveur va utiliser le service de nommage
import java.util.Properties;
import org.omg.CosNaming.*;
//inclure le package des exceptions pouvant etre generees
// par le service de nommage
import org.omg.CosNaming.NamingContextPackage.*;

// Classe démarrant l'implémentation
public class Server
{
	public static void main(String args[]) {

/*
    try{

      // create and initialize the ORB
      ORB orb = ORB.init(args, null);

      // get reference to rootpoa & activate the POAManager
      POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
      rootpoa.the_POAManager().activate();

      // create servant and register it with the ORB
      DictionnaryImpl helloImpl = new       DictionnaryImpl();
      helloImpl.setORB(orb);
      // get object reference from the servant
      org.omg.CORBA.Object ref = rootpoa.servant_to_reference(helloImpl);
      Dictionnary href = DictionnaryHelper.narrow(ref);

      // get the root naming context
      // NameService invokes the name service
      org.omg.CORBA.Object objRef =
          orb.resolve_initial_references("NameService");
      // Use NamingContextExt which is part of the Interoperable
      // Naming Service (INS) specification.
      NamingContextExt ncRef1 = NamingContextExtHelper.narrow(objRef);
      // bind the Object Reference in Naming
      String name = "Dictionnary";
      NameComponent nc1 = new NameComponent("Dictionnary","dictionnary_context");
      NameComponent nc2 = new NameComponent("Dictionnary","Object");

      NameComponent path1[] = {nc1};
      NameComponent path2[] = {nc2};
      //NameComponent path[] = ncRef.to_name( name );
      System.out.println("pass1");
      NamingContext ncRef2= ncRef1.bind_new_context(path1);
      System.out.println("pass2");
      ncRef2.rebind(path2, href);
      System.out.println("pass3");
      System.out.println("Server ready and waiting ...");
      // wait for invocations from clients
      orb.run();
    }

      catch (Exception e) {
        System.err.println("ERROR: " + e);
        e.printStackTrace(System.out);
      }

      System.out.println("Server Exiting ...");
      */
// On initialise l'ORB, puis le POA
            Properties props = new Properties();


 props.setProperty("org.omg.CORBA.ORBInitialHost", "192.168.1.27");
 props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
		ORB orb = ORB.init(args, props);
		POA rootPOA;
		POAManager manager;
                DictionnaryImpl	hello =null;
                Dictionnary	refDict=null;
 		try {
 			rootPOA = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
	    		manager = rootPOA.the_POAManager();
                        hello= new DictionnaryImpl(rootPOA);
			// On instancie une nouvelle implémentation de HelloWorld
			
                        hello.setORB(orb);
			// Activation du servant
      			//byte[] servantId = rootPOA.activate_object(hello);
      			// Récupération de la référence du servant
      			//org.omg.CORBA.Object objRefHello = rootPOA.id_to_reference(servantId);
      			// Exportation de la référence
      			//String reference = orb.object_to_string(objRefHello);
                        refDict=hello._this(orb);

                      
			// recuperer le service de nommage (= objet CORBA)
       			org.omg.CORBA.Object objRef= orb.resolve_initial_references("NameService");
       			// convertir cet objet CORBA en objet Java afin
       			NamingContext ncRef = NamingContextHelper.narrow(objRef);
			// preparer un context
			NameComponent nc = new NameComponent("Dictionnary","dictionnary_context");
       			NameComponent path[] = {nc};
			NamingContext testContext;
			try {
				testContext = ncRef.bind_new_context(path);
    			} catch(AlreadyBound ex) {
      			// If the context already exists, this exception will be raised.
      			// In this case, just resolve the name and assign testContext
      			// to the object returned:
      			org.omg.CORBA.Object objtemp = ncRef.resolve(path);
      			testContext = NamingContextHelper.narrow(objtemp);
      			if( testContext == null ) {
         			System.out.println("Failed to narrow naming context.");
         			return;
      			}
    			}
       			// preparer un composant qui contient un nom symbolique de l'objet
       			// pas d'espace entre " et "
       			nc = new NameComponent("Dictionnary","Object");
			NameComponent path2[] = {nc};
       			// enregistrer le composant dans le service de nommage
       			//testContext.rebind(path2, objRefHello);
                        testContext.rebind(path2, refDict);
        		// Activation du POA par son manager
        		manager.activate();
        		System.out.println("Serveur Java en attente...");
        		// Lancement de l'ORB
        		orb.run();
      		} catch (Exception e3) {
        		System.out.println("Exception !");
        		e3.printStackTrace();
      		}
  }

};
